package cn.ante.ymcc.web.controller;

import cn.ante.ymcc.dto.KillParamDto;
import cn.ante.ymcc.service.IKillCourseService;
import cn.ante.ymcc.domain.KillCourse;
import cn.ante.ymcc.query.KillCourseQuery;
import cn.ante.ymcc.result.JSONResult;
import cn.ante.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
     * 查询秒杀课程详情页中的秒杀信息
     */
    @RequestMapping(value = "/online/one/{killId}/{activityId}",method = RequestMethod.GET)
    public JSONResult onlineOne(@PathVariable("killId") Long killId, @PathVariable("activityId") Long activityId){
        return JSONResult.success(killCourseService.onlineOne(killId,activityId));
    }

    /* 立即秒杀  进行下单
     *
     */
    @RequestMapping(value = "/kill",method = RequestMethod.POST)
    public JSONResult kill(@RequestBody KillParamDto dto){
        return JSONResult.success(killCourseService.kill(dto));
    }

    /**
     * 查询活动的所有秒杀课程
     */
    @RequestMapping(value = "/online/all",method = RequestMethod.GET)
    public JSONResult indexKillcourse(){
        return JSONResult.success(killCourseService.indexKillcourse());
    }


    /**
     * 渲染秒杀结算页面  点击秒杀下单后就生成
     */
    @RequestMapping(value = "/online/oneByOrder/{orderNo}",method = RequestMethod.GET)
    public JSONResult queryPreOrder(@PathVariable("orderNo") String orderNo){
        return JSONResult.success(killCourseService.queryPreOrder(orderNo));
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }

    /**
    * @Description: 普通课程加入秒杀课程中
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/8/23 20:22
    */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JSONResult addKillCourse(@RequestBody KillCourse killCourse){
        killCourseService.addKillCourse(killCourse);
        return JSONResult.success();
    }
}
